const db = require('../db/db_config');
exports.getUsers = (req, res) => {
  let sql = 'select * from user';
  db.query(sql, (err, results) => {
    if (err) {
      return res.json({
        status: 0,
        message: err.message
      });
    }
    res.json({
      code: 200,
      message: '用户查询成功',
      data: results
    })
  })
}


exports.register = (req, res) => {
  const user = req.body;
  let sql1 = "select * from user where name = ?";
  db.query(sql1, [user.name], (err, results) => {
    if (err) {
      return res.json({
        status: 0,
        message: err.message
      })
    }
    if (results.length > 0) {
      return res.json({
        status: 0,
        message: '用户名已存在'
      })
    }
    let sql2 = "insert into user set ?";
    db.query(sql2, user, (err, results) => {
      if (err) {
        return res.json({
          status: 0,
          message: err.message
        })
      }
      res.json({
        status: 1,
        message: '用户注册成功'
      })
    })


  })
}
